<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class VerifyLogin extends CI_Controller {

  private $username;	
	
 function __construct(){
   parent::__construct();
   $this->load->model('user_model','',TRUE);
   $this->load->helper(array('form', 'url'));
   $this->load->library('form_validation');
   $this->username = $this->input->post('username');
 }

	 function index(){
	   
	
	   $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
	   $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
	
	   
	   
	   if($this->form_validation->run() == FALSE){
	        $this->load->view('login/login.php');
	   }
	   else{
	     redirect('index.php/admin', 'refresh'); 
	   }
	
	 }

	 function check_database($password){
		   $result = $this->user_model->login($this->username, $password);
         
		   if($result){
		     $sess_array = array();
		     foreach($result as $row){
		          
					$sess_array = array(
						'per_id_persona' => $row->per_id_persona,
						'per_nombre' => $row->per_nombre
                       
						//'email' => $row->email
					);
					$this->session->set_userdata('logged_in', $sess_array); // nombre variable y string 
		     }
		     return true;
		   }else{
		     $this->form_validation->set_message('check_database', 'Nombre o password incorrecto');
		    // $this->load->view('login/login.php');
		     return false;
		   }
	 }
	 
	 

	 public function logout(){
	 	$this->session->unset_userdata('logged_in');
	 	session_destroy();
	 	redirect('/', 'refresh');
	 }
	
 
}